import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MiniApplicationContext {
    private Map<String, Object> beans = new HashMap<>();

    public MiniApplicationContext(String xmlPath) throws Exception {
        SAXReader reader = new SAXReader();
        Document document = reader.read(MiniApplicationContext.class.getClassLoader().getResource(xmlPath));
        Element rootElement = document.getRootElement();
        List<Element> elementList = rootElement.elements();
        for (Element element: elementList) {
            String id = element.attributeValue("id");
            String clazz = element.attributeValue("class");
            Object bean = Class.forName(clazz).newInstance();
            beans.put(id, bean);
        }
    }

    public Object getBean(String id) {
        return beans.get(id);
    }
}